<?php

namespace Applications\controllers;

use GatewayWorker\Lib\Gateway;
use Workerman\Lib\Timer;

class Test
{

    public function log($param)
    {
        \Seaslog::info('param' . json_encode($param));

        return json_encode(['status' => 0, 'rand' => mt_rand(111, 999)]);
    }

    public function login($param)
    {
        global $db;
        $result = $db->select('id,name,username,password')
            ->from('staff')
            ->where('username= :username')
            ->bindValues([
                'username' => $param['username'],
            ])
            ->row();

        if ($result != false && password_verify($param['password'], $result['password'])) {
            Gateway::bindUid($param['client_id'], $result['id']);
            Timer::del($_SESSION['auth_timer_id']);
            $_SESSION['uid'] = $result['id'];
            $_SESSION['name'] = $result['name'];
            $_SESSION['username'] = $result['username'];
            return json_encode(['status' => 0, 'msg' => 'login success', 'rand' => mt_rand(111, 999)]);
        }

        Gateway::closeClient($param['client_id']);
        return json_encode(['status' => 1, 'msg' => 'login error', 'rand' => mt_rand(111, 999)]);
    }
}